package com.laikan.reader.bookfriend.activity;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.telephony.TelephonyManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.laikan.library.components.activitys.BasicActivity;
import com.laikan.library.httpvolley.engine.HttpApiRequestEngine;
import com.laikan.library.httpvolley.engine.Requester;
import com.laikan.library.httpvolley.helper.RequestListener;
import com.laikan.library.httpvolley.helper.VolleyErrorHandler;
import com.laikan.library.httpvolley.response.HttpResponse;
import com.laikan.library.managers.ToastMgr;
import com.laikan.library.utils.SPUtil;
import com.laikan.reader.R;
import com.laikan.reader.comment.APIProtocol;
import com.laikan.reader.comment.Constants;
import com.laikan.reader.core.utils.LoadingDialog;
import com.laikan.reader.core.utils.PublicUtil;
import com.socks.library.KLog;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 兑换阅读券
 *
 * @author Administrator
 */
public class ConvertReadTicketActivity extends BasicActivity {

    private EditText et_redemption_code;
    private TextView tv_confirm_exchange;
    private TelephonyManager tm;
    private ImageView back_tv;
    private RelativeLayout back_layout;
    Drawable drawable_back_tv;
    private LoadingDialog mloadingDialog;

    @Override
    protected void assignViews() {
        setContentView(R.layout.convert_ydz);
        et_redemption_code = (EditText)findViewById(R.id.et_redemption_code);
        tv_confirm_exchange = (TextView)findViewById(R.id.tv_confirm_exchange);
        back_tv = (ImageView)findViewById(R.id.iv_back);
        back_layout = (RelativeLayout)findViewById(R.id.back_layout);
    }

    @Override
    protected void initData() {
        mloadingDialog = new LoadingDialog(mContext);
        tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        drawable_back_tv = new BitmapDrawable(PublicUtil.getAlphaBitmap(PublicUtil.readBitMap(mContext, R.drawable.lk_back_small), Color.parseColor("#66ccdd")));
        back_tv.setImageDrawable(drawable_back_tv);
    }

    @Override
    protected void setListener() {
        back_layout.setOnClickListener(this);
        // 确认兑换
        tv_confirm_exchange.setOnClickListener(this);
        // 输入字符兑换码监听
        et_redemption_code.addTextChangedListener(new TextWatcher() {

            private CharSequence temp;

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                temp = s;
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (temp.length() > 0) {
                    // 确认按钮可用
                    tv_confirm_exchange.setEnabled(true);
                }else {
                    // 确认按钮不可用
                    tv_confirm_exchange.setEnabled(false);
                }
            }
        });
    }

    @Override
    protected void onClickEvent(View view) {
        switch (view.getId()) {
            case R.id.back_layout:
                finish();
                break;
            case R.id.tv_confirm_exchange:
                convertYdz();
                break;
            default:
                break;
        }
    }

    /**
     * 兑换阅读券
     *
     * @date 2015-12-23 下午4:21:51
     * @author yjh
     */
    private void convertYdz() {
        mloadingDialog.show();
        String seq = et_redemption_code.getText().toString();
        Map<String, String> map = new HashMap<>();
        map.put("sd", SPUtil.getString("token", ""));
        map.put("seq", seq);
        map.put("deviceId", tm.getDeviceId());

        RequestListener requestListener = new RequestListener() {
            @Override
            public void onPreExecute() { KLog.e("---------volley onPreExecute!!!"); }

            @Override
            public void onSuccess(HttpResponse httpResponse) {
                mloadingDialog.dismiss();
                try {
                    JSONObject jsonObject = new JSONObject(httpResponse.responseContent);
                    String error_msg = jsonObject.getString("error_msg");
                    ToastMgr.showShortToast(error_msg);

                    String error_no = jsonObject.getString("error_no");
                    if (!Constants.RESULT_OK.equals(error_no)) {
                        // 领取成功
                        et_redemption_code.setText("");
                        // 确认按钮不可用
                        tv_confirm_exchange.setEnabled(false);
                    }else {
                        // 领取失败
                    }
                }catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(VolleyErrorHandler errorHandler) {
                mloadingDialog.dismiss();
                ToastMgr.showShortToast(R.string.laikan_network_anomaly);
            }

            @Override
            public void onFinish() {

            }
        };

        Requester requester = new Requester();
        requester.setUrl(APIProtocol.laikan_yueduquan_convert);
        requester.setMapBody(map);
        HttpApiRequestEngine.getInstance().httpPostRequest(requester, requestListener);

//        HttpApiRequestEngine.getInstance().httpPostRequest(APIProtocol.laikan_yueduquan_convert, map, null, null, requestListener);
    }

}
